package net.betou.trade.main.manager.impl;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import net.betou.common.page.Pagination;
import net.betou.common.util.TradeUtils;
import net.betou.common.web.RequestUtils;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Log;
import net.betou.trade.main.dao.LogDao;
import net.betou.trade.main.manager.LogMng;
import net.betou.trade.web.Messages;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.UrlPathHelper;

@Service
@Transactional
public class LogMngImpl implements LogMng{
	
	@Transactional( readOnly = true )
	public Pagination getPage( int category, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize ){
		return dao.getPage( category, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
	}

	@Transactional( readOnly = true )
	public Log findById( Integer id ) {
		Log entity = dao.findById( id );
		return entity;
	}

	public Log save( Integer category, AgentAndAdmins admin, String url, String ip, Date date, String title, String content ) {
		Log log = new Log( );
		String username;
		
		if( admin == null ){
			username = "例外";
		}else{
			username = admin.getUsername( ) ;
		}
		
		log.setUsername( username );
		log.setCategory( category );
		log.setIp( ip );
		log.setTime( date );
		log.setUrl( url );
		log.setTitle( title );
		log.setContent( content );
		save( log );
		return log;
	}

	public Log loginSuccess( HttpServletRequest request, AgentAndAdmins user, String title ) {
		String ip = RequestUtils.getIpAddr( request );
		UrlPathHelper helper = new UrlPathHelper( );
		String uri = helper.getOriginatingRequestUri( request );
		Date date = new Date( );
		Log log = save( Log.LOGIN_SUCCESS,  user, uri, ip, date, title, null );
		return log;
	}

	public Log loginFailure( HttpServletRequest request, String title, String content ) {
		String ip = RequestUtils.getIpAddr( request );
		UrlPathHelper helper = new UrlPathHelper( );
		String uri = helper.getOriginatingRequestUri( request );
		Date date = new Date( );
		Log log = save( Log.LOGIN_FAILURE, null, uri, ip, date, title, content );
		return log;
	}

	public Log operating( HttpServletRequest request, String title, String content ) {
		AgentAndAdmins user = TradeUtils.getAdmin( request );
		String ip = RequestUtils.getIpAddr( request );
		UrlPathHelper helper = new UrlPathHelper( );
		String uri = helper.getOriginatingRequestUri( request );
		Date date = new Date( );
		Log log = save( Log.OPERATING, user, uri, ip, date, title, content );
		return log;
	}
	
	public Log operating( int saveOrUpdate, HttpServletRequest request, String title, String content ){
		
		if( saveOrUpdate == 0 ){
			title = Messages.LOG_SAVE + title;
		}else if( saveOrUpdate == 1 ){
			title = Messages.LOG_UPDATE + title;
		}else{
			title = Messages.LOG_DELETE + title;
		}
		
		AgentAndAdmins user = TradeUtils.getAdmin( request );
		String ip = RequestUtils.getIpAddr( request );
		UrlPathHelper helper = new UrlPathHelper( );
		String uri = helper.getOriginatingRequestUri( request );
		Date date = new Date( );
		
		Log log = save( Log.OPERATING, user, uri, ip, date, title, content );
		
		return log;
	}

	public Log save( Log bean ) {
		dao.save( bean );
		return bean;
	}

	public int deleteBatch( Integer category, Integer days ) {
		Date date = null;
		if( days != null && days > 0 ){
			Calendar cal = Calendar.getInstance( );
			cal.add( Calendar.DAY_OF_MONTH, -days );
			date = cal.getTime( );
		}
		return dao.deleteBatch( category,  date );
	}

	public Log deleteById( Integer id ) {
		Log bean = dao.deleteById( id );
		return bean;
	}

	public Log [ ] deleteByIds( Integer [ ] ids ) {
		Log [ ] beans = new Log [ ids.length ];
		for ( int i = 0, len = ids.length; i < len; i++ ){
			beans[ i ] = deleteById( ids[ i ] );
		}
		return beans;
	}

	@Autowired
	private LogDao dao;
	


}